package com.evon.yardmanagement.ui.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.evon.yardmanagement.model.Transporter;
import com.evon.yardmanagement.model.Yard;
import com.evon.yardmanagement.service.MemberService;
import com.evon.yardmanagement.service.OEMService;
import com.evon.yardmanagement.service.YardManagementService;
import com.evon.yardmanagement.ui.dto.MemberDTO;
import com.evon.yardmanagement.ui.dto.OemDTO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;

public class ManageMemberAction extends ActionSupport implements
		ApplicationAware {

	/**
		 * 
		 */
	private static final long serialVersionUID = 1L;

	Map<String, Object> globalData;

	private List<MemberDTO> listMember = new ArrayList<MemberDTO>();
	private MemberDTO memberDTO;
	
	public ManageMemberAction(){
		memberDTO = new MemberDTO();
	}

	public MemberDTO getMemberDTO() {
		return memberDTO;
	}
	@SkipValidation
	public void setMemberDTO(MemberDTO memberDTO) {
		this.memberDTO = memberDTO;
	}

	public List<MemberDTO> getListMember() {
		return listMember;
	}
	@SkipValidation
	public void setListMember(List<MemberDTO> listMember) {
		this.listMember = listMember;
	}

	private int selectedYardId;
	public List<Yard> listYard = new ArrayList<Yard>();

	private List<OemDTO> oemList;
	
	private int oemType;
	
	public List<OemDTO> getOemList() {
		return oemList;
	}
	@SkipValidation
	public void setOemList(List<OemDTO> oemList) {
		this.oemList = oemList;
	}

	public int getOemType() {
		return oemType;
	}
	@SkipValidation
	public void setOemType(int oemType) {
		this.oemType = oemType;
	}
	@SkipValidation
	public String init() {
		oemList = OEMService.getAllOem();
		//Addded for back button
		if (getSelectedYardId()!=0) {
			listYard = new YardManagementService().getYardsByOemId(oemType);	
			listMember = new MemberService().getMembersOfYard(selectedYardId);
			globalData.put("YARDS", listYard);
		}
		//end
		memberDTO = new MemberDTO();
		globalData.put("oemList", oemList);
		
		return SUCCESS;
	}
	@SkipValidation
	public  String populateYard() {
		oemList = (List<OemDTO>)globalData.get("oemList");
		listYard = new YardManagementService().getYardsByOemId(oemType);
		memberDTO = new MemberDTO();
		globalData.put("YARDS", listYard);
		return SUCCESS;
	}
	@SkipValidation
	public String execute() {
		System.err.println("addsds" + selectedYardId);
		oemList = (List<OemDTO>) globalData.get("oemList");
		listYard = (List<Yard>) globalData.get("YARDS");
		listMember = new MemberService().getMembersOfYard(selectedYardId);
		return SUCCESS;
	}

	/**
	 * 
	 * @return
	 */
	@SkipValidation
	public String showDetails() {
		listYard = (List<Yard>) globalData.get("YARDS");
		if(memberDTO.getId() > 0)
			memberDTO = new MemberService().getMemberDetails(memberDTO.getId());
		return SUCCESS;
	}
	
	@Validations(
			requiredStrings = { 
					@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "memberDTO.name", message = "Please enter Member First Name."),
					@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "memberDTO.middleName", message = "Please enter Member Middle Name."),
					@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "memberDTO.lastName", message = "Please enter Member Last Name."),
					@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "memberDTO.city", message = "Please enter City."),
					@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "memberDTO.phone", message = "Please enter Phone."),
					@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "memberDTO.address", message = "Please enter Address."),
					@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "memberDTO.type", message = "Please enter Member Type.")
			},
			regexFields = {					
					@RegexFieldValidator(fieldName ="memberDTO.name", expression = "^[a-zA-Z]+$", message = "Only Character is allowed for First name."),
					@RegexFieldValidator(fieldName ="memberDTO.middleName", expression = "^[a-zA-Z]+$", message = "Only Character is allowed for Middle name."),
					@RegexFieldValidator(fieldName ="memberDTO.lastName", expression = "^[a-zA-Z]+$", message = "Only Character is allowed for Lsat name."),
					@RegexFieldValidator(fieldName ="memberDTO.city", expression = "^[a-zA-Z]+$", message = "Only Character is allowed for City name."),
					@RegexFieldValidator(fieldName ="memberDTO.address", expression = "^[a-zA-Z]+$", message = "Only Character is allowed for Address."),
					@RegexFieldValidator(fieldName="memberDTO.phone", expression="^[0-9]+",message="Phone can only be numeric", type=ValidatorType.FIELD)
					//@RegexFieldValidator(fieldName ="memberDTO.middleName", expression = "^[a-zA-Z]+$", message = "Only Character is allowed for Middle name."),
					//@RegexFieldValidator(fieldName ="sectionDTO.capacity", expression = "^[0-9]+", message = "Slot can only be numeric")
					}
	)
	public String addMember(){
		listYard = (List<Yard>) globalData.get("YARDS");
		
		/*if(!isValidInput(memberDTO)){
			return INPUT;
		}*/
		
		memberDTO = new MemberService().addMember(memberDTO, selectedYardId);
		addActionMessage("Member Added successfully");
		return SUCCESS;
	}

	private boolean isValidInput(MemberDTO memberDTO2) {
		//Validate all input parameters.
		if(memberDTO.getName() == null || memberDTO.getName().trim().equals("")){
			addActionError("Please enter Member First Name");
			return false;
		}
		
		if(memberDTO.getMiddleName() == null || memberDTO.getMiddleName().trim().equals("")){
			addActionError("Please enter Member Middle Name");
			return false;
		}
		
		if(memberDTO.getLastName() == null || memberDTO.getLastName().trim().equals("")){
			addActionError("Please enter Member Last Name");
			return false;
		}
		if(memberDTO.getCity() == null || memberDTO.getCity().trim().equals("")){
			addActionError("Please enter City");
			return false;
		}
		
		if(memberDTO.getPhone() == null || memberDTO.getPhone().trim().equals("")){
			addActionError("Please enter Phone");
			return false;
		}
		
		if(memberDTO.getAddress() == null || memberDTO.getAddress().trim().equals("")){
			addActionError("Please enter Address");
			return false;
		}
		if(memberDTO.getType() == null || memberDTO.getType().trim().equals("")){
			addActionError("Please enter Member Type");
			return false;
		}

		return true;
	}

	public int getSelectedYardId() {
		return selectedYardId;
	}
	@SkipValidation
	public void setSelectedYardId(int selectedYardId) {
		this.selectedYardId = selectedYardId;
	}
	@SkipValidation
	public void setApplication(Map<String, Object> application) {
		globalData = application;

	}

}
